package com.jyu.sati.business.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jyu.sati.business.service.HomeService;
import com.jyu.sati.common.util.AjaxResult;
import com.jyu.sati.entity.Document;
import com.jyu.sati.entity.Role;

/**
 * 首页管理控制器
 * 
 * @author 淋雨又调皮
 *
 */
@RequestMapping("/home")
@Controller
public class HomeController extends BaseController {

	@Autowired
	private HomeService homeService;

	/**
	 * 删除首页宣传图片
	 * 
	 * @param documentId
	 *            图片id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/deleteHomeImage", method = { RequestMethod.DELETE })
	public AjaxResult deleteHomeImage(@RequestBody Document image) {
		AjaxResult result = getAjaxResult();
		if (!checkIsLogin()) {
			return getUnLoginResult(result);
		}
		if (hasNotRole(Role.ROLE_NAME_PLATFORM) && hasNotRole(Role.ROLE_NAME_SUPER_ADMIN)) {
			result = getNoRoleResult(result);
		}
		try {
			homeService.deleteHomeImage(image.getDocumentId());
			result = getSuccessRusult(result, "删除首页宣传图片成功！");
		} catch (Exception e) {
			result = getFailedRusult(result, "删除首页宣传图片失败!");
		}
		return result;
	}

	/**
	 * 获取所有首页宣传图片
	 * 
	 * @param documentId
	 *            图片id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getAllHomePageImages", method = { RequestMethod.GET })
	public AjaxResult getAllHomePageImages() {
		AjaxResult result = getAjaxResult();
		try {
			result = getSuccessListRusult(result, "获取所有首页宣传图片成功！", homeService.getAllHomeImages());
		} catch (Exception e) {
			result = getFailedRusult(result, "获取所有首页宣传图片失败!");
		}
		return result;
	}

}
